package org.apache.ocean.main.merge;

import org.apache.lucene.util.PriorityQueue;
import org.apache.ocean.main.IndexDocuments.IndexDocument;
import org.apache.ocean.main.search.Search.Sort;

public class MergeSortedQueue extends PriorityQueue {
	private Sort sort;

	public MergeSortedQueue(Sort sort, int size) {
		this.sort = sort;
		initialize(size);
	}

	protected final boolean lessThan(final Object a, final Object b) {
		IndexDocument docA = (IndexDocument) a;
		IndexDocument docB = (IndexDocument) b;
		final int n = sort.size();
		int c = 0;
		for (int i = 0; i < n && c == 0; ++i) {
			Sort.Field sortField = sort.get(i);
			String field = sortField.getName();
			Comparable comparableA = (Comparable)docA.get(field);
			Comparable comparableB = (Comparable)docB.get(field);
			c = comparableA.compareTo(comparableB);
			if (sort.get(i).isReverse()) {
				c = -c;
			}
		}
		return c > 0;
	}
}
